package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_token;

import cn.com.api.service.ApiT_tokenService;

import cn.com.util.*;
import cn.com.util.*;

public class T_tokenBaseController {

	private static Logger logger = Logger
			.getLogger(T_tokenBaseController.class);

	@Autowired
	private ApiT_tokenService apiT_tokenService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_token-list")
	public ModelAndView t_token_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_token_list.jsp");

		List<ApiT_token> t_tokens = apiT_tokenService.findT_tokens();
		mv.addObject("t_tokens", t_tokens);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_token-add-pre")
	public ModelAndView t_token_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_token_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_token t_token = apiT_tokenService.findT_tokenById(Integer
					.valueOf(id));
			mv.addObject("t_token", t_token);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_token-add")
	public void t_token_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String userid = request.getParameter("userid");
		String memberid = request.getParameter("memberid");
		String token = request.getParameter("token");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String role = request.getParameter("role");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_token t_token = new ApiT_token();
			/*
			 * t_token.setId(id); t_token.setUserid(userid);
			 * t_token.setMemberid(memberid); t_token.setToken(token);
			 * t_token.setLongitude(longitude); t_token.setLatitude(latitude);
			 * t_token.setRole(role);
			 */
			apiT_tokenService.insertT_token(t_token);
		}
		// edit
		else {
			ApiT_token t_token = apiT_tokenService.findT_tokenById(Integer
					.valueOf(id));
			/*
			 * t_token.setId(id); t_token.setUserid(userid);
			 * t_token.setMemberid(memberid); t_token.setToken(token);
			 * t_token.setLongitude(longitude); t_token.setLatitude(latitude);
			 * t_token.setRole(role);
			 */
			apiT_tokenService.updateT_token(t_token);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_token-delete")
	public void t_token_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_tokenService.deleteT_token(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
